package cn.iocoder.yudao.server;

import cn.hutool.core.date.StopWatch;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.iocoder.yudao.module.pay.dal.dataobject.demo.PayDemoOrderDO;
import com.google.common.base.CaseFormat;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

public class EntityCaseTableUtils {
    public static void main(String[] args) {

        LocalDateTime now = LocalDateTime.now();
//        now = LocalDateTime.of(2024, 1, 29, 0 , 0);
//        LocalDateTime startOfDay = now.with(LocalTime.MIN);
//        LocalDateTime endOfDay = now.with(LocalTime.MAX);
//        endOfDay = endOfDay.plusMonths(1).minusDays(1);
//
//        System.out.println(startOfDay);
//        System.out.println(endOfDay);

        String sCurTime = "20220303";

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate curTimeLDT = LocalDate.parse(sCurTime, df);
        System.out.println(curTimeLDT);

        DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM");
        System.out.println("-----------" + df1.format(LocalDate.now()));

        LocalDateTime dateTime = LocalDateTime.now();
        dateTime = curTimeLDT.atStartOfDay();
        LocalDateTime starTime = dateTime.with(LocalTime.MIN);
        LocalDateTime endTime = dateTime.with(LocalTime.MAX).plusMonths(1).minusDays(1).withNano(0);
//        endTime = endTime.withNano(0);
        System.out.println(starTime + "\t" + endTime);
        System.out.println(starTime + "\t" + endTime);
//        generateCreateTableSQL();
//        Long storeId = 1L;
//        System.out.println(storeId.equals(null));

//        for (int i = 0; i < 10; i++) {
//            System.out.println(IdUtil.getSnowflake(1, 1).nextId() + "\t" +IdUtil.objectId());
//        }
//
//        StopWatch stopWatch = new StopWatch("while循环执行时间统计");
//        stopWatch.start("循环1");
//        Map<String, String> couponMap = Maps.newHashMap();
//        for (int i = 0; i < 1000; i++) {
//            String randomStr = RandomUtil.randomString(10);
////            randomStr = SecureUtil.md5(randomStr);
//            System.out.println(randomStr);
//            couponMap.put(randomStr, "1");
//        }
//        stopWatch.stop();
//        System.out.println(couponMap.size());
//        System.out.println("task count:" + stopWatch.getTaskCount());
//        System.out.println("last task name:" + stopWatch.getLastTaskName());
//        System.out.println("total time:" + stopWatch.getTotalTimeMillis() + " ms");
//        System.out.println("last task time:" + stopWatch.getLastTaskTimeMillis() + " ms");
    }

    private static void generateCreateTableSQL() {
        Class<PayDemoOrderDO> entityClass = PayDemoOrderDO.class;
        com.baomidou.mybatisplus.annotation.TableName description = entityClass.getAnnotation(com.baomidou.mybatisplus.annotation.TableName.class);

        String tableName = description.value();
        StringBuilder sql = new StringBuilder();
        sql.append("CREATE TABLE ").append(tableName).append(" (\n");

        Field[] fields = entityClass.getDeclaredFields();
        for (Field field : fields) {
            String fieldName = field.getName();
            String fieldType = field.getType().getSimpleName();

            // 根据字段类型转换为数据库对应的数据类型
            String dbType = convertToDbType(fieldType);
            String filedStr = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, fieldName);
            // 添加字段和数据类型到SQL语句中
            if (StringUtils.equals("id", fieldName)) {
                sql.append("\t").append(filedStr).append(" ").append(dbType).append(" NOT NULL AUTO_INCREMENT").append(",\n");
            } else {
                sql.append("\t").append(filedStr).append(" ").append(dbType).append(",\n");
            }
        }
        sql.append("`creator` varchar(64)  DEFAULT '' COMMENT '创建者',\n" +
                "  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',\n" +
                "  `updater` varchar(64)  DEFAULT '' COMMENT '更新者',\n" +
                "  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',\n" +
                "  `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',\n" +
                "  `tenant_id` bigint NOT NULL DEFAULT '0' COMMENT '租户编号',\n");

        // 添加主键约束
        sql.append("\tPRIMARY KEY (id)\n");

        sql.append(");");

        System.out.println(sql.toString());
    }

    private static String convertToDbType(String fieldType) {
        Map<String, String> typeMap = new HashMap<>();
        typeMap.put("boolean", "BOOLEAN");
        typeMap.put("byte", "TINYINT");
        typeMap.put("short", "SMALLINT");
        typeMap.put("int", "INT");
        typeMap.put("Integer", "INT");
        typeMap.put("long", "BIGINT");
        typeMap.put("Long", "BIGINT");
        typeMap.put("float", "FLOAT");
        typeMap.put("double", "DOUBLE");
        typeMap.put("char", "CHAR(1)");
        typeMap.put("String", "VARCHAR(255)");
        typeMap.put("Date", "DATETIME");
        typeMap.put("LocalDateTime", "DATETIME");
        // 可以继续添加其他类型的映射关系

        String dbType = typeMap.get(fieldType);

        if (dbType == null) {
            dbType = "VARCHAR(255)";
        }

        return dbType;
    }
}

